﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;

namespace PMS
{
    public class SecurityHelper
    {
        #region 1.0使用票据对象，将用户对象加密成字符串
        /// <summary>
        /// 使用票据对象，将用户对象加密成字符串
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public static string EncryptUserInfo(string userInfo)
        {
            //1.1将用户数据存入票据对象
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, "哈哈", DateTime.Now, DateTime.Now, true, userInfo);
            //1.2将票据对象加密成字符串
            string strData = System.Web.Security.FormsAuthentication.Encrypt(ticket);
            return strData;
        }
        #endregion

        #region 2.0将加密字符串解密
        /// <summary>
        /// 将加密字符串解密
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public static string DecryptUserInfo(string cryptograph)
        {
            FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cryptograph);

            return ticket.UserData;
        }
        #endregion
    }
}